package ClassData.Visitor.MemoryMapper;

import ClassData.ClassData;
import ClassData.ClassDataList;
import ClassData.Visitor.IVisitor;

/**
 * Gain some statistics from ClassDataList.
 * @author Dominik Ollesz
 * @version 0.1
 */
public class StatisticGenerator extends AbstractVisitor implements IVisitor {
	
	private Statistic statistic = new Statistic();
	
	public StatisticGenerator() {
		super(""); // no output to file
	}
	
	public StatisticGenerator(String path) {
		super(path);
	}

	public void calculateStatistic(ClassDataList myClassDataList) {
		open();
		visit_ClassDataList(myClassDataList);
		close();
	}
	
	// functionality goes here:
	
	@Override
	public void visit_ClassData(ClassData classData) {
		// update general statistic
		statistic.addClasses(1);
		statistic.addMethods(classData.getMethodsCount());
		statistic.addAttributes(classData.getAttributeCount());
	}

	@Override
	public void visit_ClassDataList(ClassDataList classDataList) {
		statistic.clear();
		for (ClassData classData : classDataList) {
			classData.receive_Visitor(this);
		}
		write("Statistic: " + statistic);
	}

	// inner class for general statistics.
	public class Statistic {
		private int classes;
		private int methods;
		private int attributes;
		
		public Statistic() {
			clear();
		}
		
		int getClassCount() {
			return classes;
		}
		
		int getMethodCount() {
			return methods;
		}
		
		int getAttributeCount() {
			return attributes;
		}
		
		void addClasses(int classCount) {
			classes += classCount;
		}
		
		void addMethods(int methodCount) {
			methods += methodCount;
		}
		
		void addAttributes(int attributeCount) {
			attributes += attributeCount;
		}
		
		void clear() {
			classes    = 0;
			methods    = 0;
			attributes = 0;
		}
		
		@Override
		public String toString() {
			return "" + classes    + " classes /" 
			          + methods    + " methods /" 
			          + attributes + " attributes";
		}
	}
	
}
